<template>
	<view class="page" :style="{'min-height':h+'px','padding-top':mt+'px'}">
		<!-- 头部 -->
		<c-nav-bar title="申请退票退款" bgColor="#007A69" leftIconColor="#ffffff"
			:titleStyle="{'fontSize': '36rpx','fontWeight': 'bold','color': '#ffffff'}"></c-nav-bar>
		<!-- 	<image class="bg" src="https://i.ringzle.com/file/20231025/87b08695ad8e42418fd81a98b2f742fe.jpg" mode="">
		</image> -->
		<view class="bg">

		</view>
		<!-- 船票信息 -->
		<view class="box ship">
			<view class="s_top">
				<view class="st_left">
					<text>{{item.date}}</text>
					<text>{{item.week}}</text>
				</view>
				<view class="st_right" v-if="item.timeMemosStr||item.timeMemos">
					<image src="@/static/my/icon_time.png"></image>
					<text>{{item.timeMemosStr||item.timeMemos}}</text>
				</view>
			</view>
			<view class="s_middle">
				<view class="sm_left">
					<text>{{item.time}}</text>
					<view class="fromto">
						<image src="@/static/index/steamerTicket/icon_line2.png"></image>
						<view class="fo_addr">
							<text>{{item.startPortName}}</text>
							<text>{{item.endPortName}}</text>
						</view>
					</view>
				</view>
				<view class="sm_img" @tap="toAddress">
					<image src="https://i.ringzle.com/file/20231025/6f507aa673cb412880fd6181c38d7e1b.png"></image>
					<text>港口地址</text>
				</view>
			</view>
			<view class="s_bottom">
				<view>
					<text>{{item.lineNum+''+item.sx}}</text>
					<text>{{item.clxm}}</text>
				</view>
				<view v-if="car">
					<text>车船联票：开车时间13:20</text>
					<text>开船时间：15:30</text>
				</view>
			</view>
		</view>
		<!-- 全选 -->
		<view class="box allselect">
			<view class="as_top">
				<image src="https://fsy.shengsi.gov.cn/file/20240906/119a9318c011420ba8b7677bc12fbf00.png" mode="" v-if="!isAll" @tap="allSelectOrNot"></image>
				<image src="@/static/my/icon_select.png" mode="" v-else @tap="allSelectOrNot"></image>
				<text>全选</text>
			</view>
			<view class="line"></view>
			<view class="as_item" v-for="(passenger,index) in passengers" :key="index">
				<image src="https://fsy.shengsi.gov.cn/file/20240906/119a9318c011420ba8b7677bc12fbf00.png" v-if="!passenger.isSelect"
					@tap="selectItem(passenger,index)"></image>
				<image src="@/static/my/icon_select.png" v-else @tap="selectItem(passenger,index)"></image>
				<view class="asi_info">
					<view class="info">
						<view class="left">
							<text class="name">{{passenger.passName}}</text>
							<text class="zjlx">{{passenger.zjName}}</text>
							<text class="card">{{passenger.credentialNum}}</text>
							<view class="type">{{personTypeCfg[passenger.priceType]||''}}</view>
						</view>
						<view class="right">￥{{passenger.realFee}}</view>
					</view>
					<view class="refund">
						<text class="text">预计退款金额</text>
						<text class="money">￥{{passenger.isSelect?(passenger.refundFee||0):0}}</text>
					</view>
					<view class="tip">退票比例{{passenger.refundScale}}%，预估手续费￥{{passenger.refundCharge||0}}</view>
				</view>
			</view>
		</view>
		<!-- 退款原因 -->
		<view class="box refund">
			<text class="title">退款原因</text>
			<view class="r_item" v-for="(item,index) in refundReasons" :key="index">
				<text>{{item.title}}</text>
				<image src="https://fsy.shengsi.gov.cn/file/20240906/119a9318c011420ba8b7677bc12fbf00.png" mode="" v-if="!item.isSelect"
					@tap="selectReason(item,index)"></image>
				<image src="@/static/my/icon_select.png" mode="" v-else @tap="selectReason(item,index)"></image>
			</view>
		</view>
		<!-- 底部 -->
		<view class="bottom">
			<view class="b_left">
				<view>退款金额：<span>￥</span><text>{{price}}</text></view>
			</view>
			<view class="b_right">
				<view class="br_tk" @tap="toRefund">确认退款</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'refundTicket',
		data() {
			return {
				h: uni.getWindowInfo().windowHeight - 60,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				titleStyle: {
					fontSize: '36rpx',
					fontWeight: "bold",
					color: "#FFFFFF"
				},
				car: false,
				item: null,
				ocfg: this.$ocfg,
				week: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
				isAll: false,
				passengers: [],
				refundReasons: [{
						title: '购票错误',
						isSelect: false
					},
					{
						title: '行程有变',
						isSelect: false
					}
				],
				price: 0,
				refundReason: '',
				portCfg:{
					'1010':{lng:122.425225,lat:30.738996,title:'李柱山码头',address:'浙江省舟山市嵊泗县李五线嵊泗列岛风景名胜区',telphone:'(0580)5078323/(0580)5083723'},
					'1011':{lng:122.425225,lat:30.738996,title:'李柱山码头',address:'浙江省舟山市嵊泗县李五线嵊泗列岛风景名胜区',telphone:'(0580)5078323/(0580)5083723'},
					'1012':{lng:122.465182,lat:30.729425,title:'小菜园客运码头',address:'浙江省舟山市嵊泗县海滨东路265号',telphone:'(0580)5070342'},
					'1013':{lng:122.699829,lat:30.846601,title:'嵊泗(绿华)',address:'浙江省舟山市嵊泗县花鸟岛内(东侧)',telphone:'(0580)5596999'},
					'1014':{lng:122.699829,lat:30.846601,title:'花鸟客运码头',address:'浙江省舟山市嵊泗县花鸟岛内(东侧)',telphone:'(0580)5596999'},
					'1015':{lng:122.699829,lat:30.846601,title:'嵊泗(毕下)',address:'浙江省舟山市嵊泗县花鸟岛内(东侧)',telphone:'(0580)5596999'},
					'1016':{lng:122.80995,lat:30.724053,title:'嵊山北码头',address:'浙江省舟山市嵊泗县嵊山枸杞景区内(东侧)',telphone:''},
					'1017':{lng:122.757875,lat:30.708894,title:'枸杞岛干斜码头',address:'浙江省舟山市嵊泗县轧轧洞',telphone:''},
					'1018':{lng:122.092689,lat:30.591915,title:'大洋站码头',address:'浙江省舟山市嵊泗县大洋镇后门村',telphone:''},
					'1019':{lng:122.52484,lat:30.70048,title:'五龙',address:'浙江省舟山市嵊泗县沿边路73号附近',telphone:''},
					'1020':{lng:122.229553,lat:30.241081,title:'岱山竹屿码头',address:'浙江省舟山市岱山县526国道与采霞路交叉口东140米',telphone:''},
					'1022':{lng:122.118523,lat:30.138428,title:'舟山三江码头',address:'浙江省舟山市定海区马岙镇定马线',telphone:'(0580)8081001'},
					'1027':{lng:122.371892,lat:29.951295,title:'朱家尖蜈蚣峙码头',address:'浙江省舟山市普陀区朱家尖镇普渡路4号',telphone:'(0580)3889090'},
					'1028':{lng:122.134244,lat:30.600722,title:'沈家湾客运码头',address:'浙江省舟山市嵊泗县洋山深水港东端',telphone:'(0580)5298323'},
					'1030':{lng:122.285296,lat:30.433887,title:'衢山客运中心',address:'浙江省舟山市岱山县蓬莱路91号',telphone:'(0580)4372611'},
					'1031':{lng:122.177286,lat:30.342718,title:'燕窝山客运中心',address:'浙江省舟山市岱山县燕窝山码头',telphone:''},
					'1034':{lng:122.55166,lat:30.665056,title:'黄龙码头',address:'浙江省舟山市嵊泗县',telphone:''},
					'1036':{lng:121.317611,lat:31.194301,title:'上海长途客运虹桥站',address:'上海市闵行区申虹路298号',telphone:'(021)51245555'},
					'1041':{lng:121.53371,lat:29.861731,title:'宁波汽车南站',address:'浙江省宁波市海曙区甬水桥路408号',telphone:'(0574)87133561'},
					'1046':{lng:121.501345,lat:31.208135,title:'上海南浦旅游集散中心',address:'上海市黄浦区外马路1588号',telphone:'4008872626'},
					'1053':{lng:122.216957,lat:30.255614,title:'岱山县长途客运中心',address:'浙江省舟山市岱山县衢山大道999号',telphone:'(0580)4472521'},
					'1054':{lng:122.099067,lat:30.297928,title:'岱山旅游集散中心',address:'岱山520路,岱山502路,岱山501路',telphone:''}
				}
			}
		},
		onLoad(option) {
			if (option.item) {
				this.item = JSON.parse(option.item);
				this.item.date = new Date(this.item.sailDate).Format('MM月dd日');
				this.item.week = this.week[new Date(this.item.sailDate).getDay()];
				this.item.time = new Date(new Date().Format('yyyy-MM-dd') + ' ' + this.item.sailTime).Format('hh:mm');
				this.passengers = this.item.detailDTOList;
				this.passengers.forEach((d, i) =>{
					this.$set(this.passengers[i], 'isSelect', false);
					d.zjName = this.personCardCfg[d.credentialType]||'证件号';
					d.zjName = d.zjName.length>4?(d.zjName.substr(0,4)+'...'):d.zjName;
					d.refundFee = 0;
					d.refundScale = 0;
					d.refundCharge = 0;
				});
			}
		},
		methods: {
			//全选/反选
			allSelectOrNot() {
				if(!this.isAll){
					this.isAll = true;
					this.passengers.forEach((d, i) => {
						this.$set(this.passengers[i], 'isSelect', ((d.itemState == 6 || d.itemState == 7) ? false : true));
					});
					let ids = this.passengers.filter(p => p.isSelect).map(p=> p.id);
					this.getPersonRefundInfo(ids,data=>{
						data.feeDetail.forEach(d=>{
							let fidx = this.passengers.findIndex(p=>p.id==d.orderItemId);
							if(fidx>-1){
								this.passengers[fidx].refundFee = d.refundFee.toFixed(2)||0;
								this.passengers[fidx].refundScale = (d.refundScale*100).toFixed(2)||0;
								this.passengers[fidx].refundCharge = d.refundCharge.toFixed(2)||0;
							}
						})
						this.dealPrice();
					})
				}else{
					this.isAll = false;
					this.passengers.forEach((d, i) => {
						this.$set(this.passengers[i], 'isSelect', false);
						this.passengers[i].refundFee = 0;
						this.passengers[i].refundScale = 0;
						this.passengers[i].refundCharge = 0;
					});
					this.dealPrice();
				}
			},
			selectItem(item, index) {
				if (item.itemState == 6 || item.itemState == 7) return this.$showToast('该乘客已申请退款')
				if(!this.passengers[index].isSelect){
					this.getPersonRefundInfo([item.id],data=>{
						this.$set(this.passengers[index], 'isSelect', true);
						this.passengers[index].refundFee = data.feeDetail.length>0?data.feeDetail[0].refundFee.toFixed(2):0;
						this.passengers[index].refundScale = data.feeDetail.length>0?(data.feeDetail[0].refundScale*100).toFixed(2):0;
						this.passengers[index].refundCharge = data.feeDetail.length>0?data.feeDetail[0].refundCharge.toFixed(2):0;
						this.dealPrice();
					})
				}else{
					this.$set(this.passengers[index], 'isSelect', false);
					this.passengers[index].refundFee = 0;
					this.passengers[index].refundScale = 0;
					this.passengers[index].refundCharge = 0;
					this.dealPrice();
				}
			},
			getPersonRefundInfo(ids,callback){
				this.$api.post('/api/travel/api/ship/order/wouldRefund',{
					orderId:this.item.orderId,
					orderItemIds:ids
				}).then(res=>{
					if(res.data.code===0){
						callback&&callback(res.data.data);
					}else this.$showModal(res.data.msg);
				})
			},
			dealPrice(){
				this.price = this.passengers.reduce((cur,pre)=>cur+(+pre.refundFee||0),0).toFixed(2);
				
				let snum = this.passengers.filter(p => p.isSelect == true).length;
				let kxnum = this.passengers.filter(p => p.itemState != 6 && p.itemState != 7).length;
				if (snum == kxnum) this.isAll = true;
				else this.isAll = false;
			},
			selectReason(item, index) {
				this.refundReason = item.title;
				this.refundReasons.forEach((d, i) => this.$set(this.refundReasons[i], 'isSelect', false));
				this.$set(this.refundReasons[index], 'isSelect', true);
			},
			//点击确认退款弹出提示
			toRefund() {
				let pl = this.passengers.filter(p => p.isSelect).length;
				if (pl==0) return this.$showToast('请选择退票退款人');
				if (!this.refundReason) return this.$showToast('请选择退款原因');

				let that = this;
				let orderItemIdList = [];
				let sp = this.passengers.filter(p => p.isSelect == true);
				sp.forEach(s => orderItemIdList.push(s.id));
				uni.showModal({
					title: '温馨提示',
					content: '您确定要退票吗？确认申请退票将不可撤回，再次购买可能会因为无票，影响您上岛！',
					confirmText: '确定',
					confirmColor: '#007A69',
					success(res) {
						if (res.confirm) {
							that.$api.post('/travel/api/ship/order/total/refund', {
								orderId: that.item.orderId,
								refundReason: that.refundReason,
								orderItemIdList
							}).then(res => {
								if (res.data.code === 0) {
									that.$showToast('退款成功');
									setTimeout(() => {
										uni.reLaunch({
											url: '/pagesMy/order/index?name=船票&type=0&typeIndex=0'
										})
									}, 2000)
								} else that.$showModal(res.data.msg);
							})
						}
					}
				})
			},
			//跳转港口地址
			toAddress() {
				let departPort = this.portCfg['10'+this.item.startPortNo]||'';
				let arrivePort = this.portCfg['10'+this.item.endPortNo]||'';
				if(!departPort&&!arrivePort) return this.$showToast('暂无港口地址');
				uni.navigateTo({
					url: '/pagesIndex/steamerTicket/portAddress?departPort=' + JSON.stringify(departPort) + "&arrivePort=" + JSON.stringify(arrivePort)
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		background: #F5F8FA;
		display: flex;
		align-items: center;
		flex-direction: column;
		padding-bottom: 140rpx;
		.bg {
			height: 700rpx;
			position: absolute;
			width: 100%;
			background-image: linear-gradient(to bottom, #007A69, #007A69, #F5F8FA);
		}

		.box {
			width: calc(100% - 88rpx);
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 30rpx 20rpx;
			position: relative;
		}

		.ship {
			margin-top: 48rpx;

			.s_top {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.st_left {
					text {
						font-size: 30rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #111111;

						&:last-child {
							margin-left: 16rpx;
						}
					}
				}

				.st_right {
					display: flex;
					align-items: center;

					image {
						width: 28rpx;
						height: 28rpx;
					}

					text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						margin-left: 16rpx;
					}
				}
			}

			.s_middle {
				margin-top: 28rpx;
				display: flex;
				justify-content: space-between;

				.sm_left {
					display: flex;

					&>text {
						font-size: 40rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #333333;
					}

					.fromto {
						display: flex;
						align-items: center;
						margin-left: 40rpx;

						&>image {
							width: 8rpx;
							height: 68rpx;
						}

						.fo_addr {
							margin-left: 20rpx;
							display: flex;
							flex-direction: column;

							text {
								font-size: 30rpx;
								font-family: PingFang-SC-Bold, PingFang-SC;
								font-weight: bold;
								color: #333333;

								&:last-child {
									margin-top: 30rpx;
								}
							}
						}
					}
				}

				.sm_img {
					width: 178rpx;
					height: 128rpx;
					position: relative;

					image {
						width: 100%;
						height: 100%;
					}

					text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #5778A8;
						position: absolute;
						right: 24rpx;
						bottom: 31rpx;
					}
				}
			}

			.s_bottom {
				border-top: 1rpx solid #EFEFEF;
				padding-top: 30rpx;

				&>view {
					display: flex;
					align-items: center;
					margin-top: 16rpx;

					&:first-child {
						margin-top: 0;
					}

					text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #777777;
						margin-left: 16rpx;

						&:first-child {
							margin-left: 0;
						}
					}
				}
			}
		}

		.line {
			width: 100%;
			height: 1rpx;
			background: #EFEFEF;
			margin-top: 36rpx;
		}

		.allselect {
			margin-top: 15rpx;

			.as_top {
				display: flex;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
				}

				text {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
					margin-left: 20rpx;
				}
			}

			.as_item {
				margin-top: 30rpx;
				display: flex;

				&>image {
					width: 40rpx;
					height: 40rpx;
				}

				.asi_info {
					width: calc(100% - 40rpx);
					padding-left: 26rpx;

					.info{
						display: flex;
						align-items: center;
						justify-content: space-between;
						.left{
							display: flex;
							align-items: center;
							.name{
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								font-size: 30rpx;
								color: #333333;
								line-height: 42rpx;
							}
							.zjlx{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #666666;
								line-height: 24rpx;
								margin-left: 20rpx;
							}
							.card{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #666666;
								line-height: 24rpx;
								margin-left: 10rpx;
							}
							.type{
								margin-left: 10rpx;
								background: #F5F8FA;
								border-radius: 6rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 22rpx;
								color: #94A9C8;
								line-height: 30rpx;
								padding: 1rpx 7rpx;
							}
						}
						.right{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #111111;
							line-height: 28rpx;
							text-align: right;
						}
					}
				
					.refund{
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-top: 35rpx;
						.text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 26rpx;
							color: #333333;
							line-height: 30rpx;
						}
						.money{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #FF4141;
							line-height: 30rpx;
							text-align: right;
						}
					}
					
					.tip{
						margin-top: 30rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #FF7D01;
						line-height: 24rpx;
					}
				}
			}
		}

		.kc_tip {
			width: calc(100% - 60rpx);
			padding: 19rpx 30rpx 23rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FF4141;
			text-indent: 30rpx;
			position: relative;
		}

		.refund {
			margin-top: 20rpx;

			.title {
				font-size: 32rpx;
				font-family: PingFang-SC-Bold, PingFang-SC;
				font-weight: bold;
				color: #333333;
			}

			.r_item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 31rpx 0;
				border-bottom: 1rpx solid #EFEFEF;

				text {
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
				}

				image {
					width: 36rpx;
					height: 36rpx;
				}

				&:last-child {
					padding-bottom: 0;
					border: none;
				}
			}
		}

		.order {
			margin-top: 20rpx;

			.title {
				font-size: 32rpx;
				font-family: PingFang-SC-Bold, PingFang-SC;
				font-weight: bold;
				color: #333333;
			}

			.o_item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 40rpx;

				text {
					&:first-child {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}

					&:last-child {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}
			}
		}

		.bottom {
			width: calc(100% - 60rpx);
			padding: 0 30rpx 20rpx;
			height: 112rpx;
			background: #FFFFFF;
			box-shadow: 0rpx -1rpx 8rpx 0rpx rgba(0, 0, 0, 0.06);
			position: fixed;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.b_left {
				height: 40rpx;
				display: flex;
				align-items: center;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;

				span {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #FF4141;
				}

				text {
					font-size: 48rpx;
					font-family: DINAlternate-Bold, DINAlternate;
					font-weight: bold;
					color: #FF4141;
				}
			}

			.b_right {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.br_mx {
					display: flex;
					align-items: center;

					text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
					}

					image {
						width: 24rpx;
						height: 24rpx;
						margin-left: 8rpx;
					}
				}

				.br_tk {
					width: 220rpx;
					height: 80rpx;
					// background: linear-gradient(90deg, #FF7D43 0%, #FEAC2C 100%);
					background-color: #007A69;
					border-radius: 40rpx;
					line-height: 80rpx;
					text-align: center;
					font-size: 32rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					margin-left: 24rpx;
				}
			}
		}

		.details {
			padding: 40rpx 30rpx 200rpx;

			.title {
				position: relative;
				text-align: center;

				text {
					font-size: 36rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #333333;
				}

				image {
					width: 36rpx;
					height: 36rpx;
					position: absolute;
					right: 10rpx;
					top: 50%;
					margin-top: -18rpx;
				}
			}

			.d_item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 44rpx;

				text {
					font-size: 32rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #333333;

					&.red {
						font-size: 32rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #FF4141;
					}
				}

				&.yjtk {
					margin-top: 94rpx;
				}
			}

			.d_item2 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 24rpx;

				text {
					font-size: 30rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #777777;
				}
			}
		}
	}
</style>